package com.cocos.game;

import android.content.Context;
import android.os.Build;

import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.gson.Gson;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class ZDKJUtils {
    public static Gson gson = new Gson();
    public static Locale getLocale() {
        Locale systemLocale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            systemLocale = MApplication.instance.getResources().getConfiguration().getLocales().get(0);
        } else {
            systemLocale = Locale.getDefault();
        }
        return systemLocale;
    }

    /**
     * 0时区的当前时间
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getCurrentTime(){
        // 获取0时区（UTC）的当前时间
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            ZonedDateTime utcTime = ZonedDateTime.now(ZoneId.of("UTC"));
            // 格式化日期
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            return utcTime.format(formatter);
        }else {
            // 使用 Calendar 和 TimeZone 来获取0时区的当前时间
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            Date date = calendar.getTime();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
            // 格式化日期
            return sdf.format(date);
        }
    }

    /**
     * 0时区的当前日期
     * @return yyyyMMdd
     */
    public static String getCurrentUTCDate(){

        // 获取0时区（UTC）的当前时间
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            ZonedDateTime utcTime = ZonedDateTime.now(ZoneId.of("UTC"));
            // 格式化日期
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            return utcTime.format(formatter);
        }else {
            // 使用 Calendar 和 TimeZone 来获取0时区的当前时间
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            Date date = calendar.getTime();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
            // 格式化日期
            return sdf.format(date);
        }
    }

    public static void fetchAdId(Context context, OnAdIdFetchedListener listener) {
        try {
            AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
            String adId = adInfo.getId();
            boolean isLimitAdTrackingEnabled = adInfo.isLimitAdTrackingEnabled();

            if (listener != null) {
                listener.onAdIdFetched(adId, isLimitAdTrackingEnabled);
            }
        } catch (IOException e) {
            // Handle IOException
            if (listener != null) {
                listener.onAdIdError(e.getMessage());
            }
        } catch (GooglePlayServicesNotAvailableException e) {
            // Handle Google Play Services Not Available Exception
            if (listener != null) {
                listener.onAdIdError(e.getMessage());
            }
        } catch (GooglePlayServicesRepairableException e) {
            // Handle Google Play Services Repairable Exception
            if (listener != null) {
                listener.onAdIdError(e.getMessage());
            }
        }
    }


    public interface OnAdIdFetchedListener {
        void onAdIdFetched(String adId, boolean isLimitAdTrackingEnabled);
        void onAdIdError(String errorMessage);
    }
}
